//
//  VideoChatUserView.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/3.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "VideoChatUserView.h"
@implementation VideoChatUserView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    
    ViewBorderRadius(self, 1, 1, RGB(199, 199, 197));
    
    self.layer.shadowColor = [UIColor colorWithRed:110/255.0 green:109/255.0 blue:124/255.0 alpha:1].CGColor;//阴影颜色
    self.layer.shadowOffset = CGSizeMake(0, 3);//偏移距离
    self.layer.shadowOpacity = 0.8;//不透明度
    self.layer.shadowRadius = 12;//半径
    
    
    //添加手势
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
    [self addGestureRecognizer:tapGesture];
    
    return self;
}

- (void)handleTapGestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer {
    if(self.UserVideoChatClick)
    {
        self.UserVideoChatClick(_randVideo);
    }
}

-(void)createUI
{
    _playview = [[UIView alloc] init];
    [self addSubview:_playview];
    
    [_playview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.headerImage = [[UIImageView alloc] init];
    [self addSubview:self.headerImage];
    [self.headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _nickname = [[UILabel alloc] init];
    _nickname.text = @"...";
    _nickname.font = DEFAULT_FONT(12);
    _nickname.frame = CGRectMake(2, 2, 100, 20);
    _nickname.textColor = MainColor;
    [self addSubview:_nickname];
    
    _attrButton = [[QMUIFillButton alloc] initWithFillColor:MainColor2 titleTextColor:KWhiteColor];
    [_attrButton setTitle:ASLocalizedString(@"关注")forState:UIControlStateNormal];
    _attrButton.titleLabel.font = DEFAULT_FONT(11);
    //    _attrButton.frame = CGRectMake(self.width - 40, 2, 38, 20);
    [self addSubview:_attrButton];
    [_attrButton addTarget:self action:@selector(handleAttrEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_attrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@2);
        make.right.equalTo(self).offset(-2);
        make.width.equalTo(@38);
        make.height.equalTo(@20);
    }];
    
    
}

#pragma mark - TXLivePlayListener
-(void) onPlayEvent:(int)EvtID withParam:(NSDictionary*)param
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (EvtID == PLAY_EVT_PLAY_PROGRESS) {
            
        } else if(EvtID == PLAY_EVT_PLAY_END) {
            
        }
        else if(EvtID == PLAY_EVT_PLAY_BEGIN)
        {
            self.headerImage.hidden = YES;
            NSLog(ASLocalizedString(@"开始播放%@"),self.nickname);
        }
        else if(EvtID == PLAY_ERR_NET_DISCONNECT)
        {
            NSLog(ASLocalizedString(@"播放失败%@"),self.nickname);
        }
    });
}

-(void)setRandVideo:(RandList *)randVideo
{
    _randVideo = randVideo;
    
    _userLogic = [[UserInfoLogic alloc] init];
    _userLogic.uid = StringFromInt(self.randVideo.id);
    _userLogic.delegagte = self;
    [_userLogic loadDataNoCache];
    _nickname.text = _randVideo.user_nickname;
    if(self.player1==nil)
    {
        self.player1 = [TXLivePlayer new];
        [self.player1 setupVideoWidget:self.frame containView:self.playview insertIndex:0];
        [self.player1 setMute:YES];
        self.player1.delegate = self;
        
    }
    
    self.headerImage.hidden = NO;
    [self.headerImage setImageURL:safeurl(self.randVideo.avatar)];
    [self.player1 stopPlay];
    NSLog(ASLocalizedString(@"播流地址%@"),randVideo.pull_url);
    [self.player1 startPlay:randVideo.pull_url type:PLAY_TYPE_LIVE_RTMP];
}

//用户数据加载完成
-(void)requestDataCompleted;
{
    if([self.userLogic.model.attention integerValue] == 0)
    {
        [_attrButton setFillColor:MainColor];
        [_attrButton setTitle:ASLocalizedString(@"关注")forState:UIControlStateNormal];
        ;
    }
    else
    {
        [_attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
        [_attrButton setFillColor:CLightGray];
    }
    
}

- (void)handleAttrEvent:(QMUIFillButton *)button {
    [self doAttr];
}

-(void)doAttr
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"personal_api" A:@"click_attention"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:StringFromInt(self.randVideo.id) forKey:@"id"];
    
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        
        self.userLogic.model.attention = [self.userLogic.model.attention integerValue] == 0?@"1":@"0";
        
        if([self.userLogic.model.attention integerValue] == 0)
        {
            [_attrButton setFillColor:MainColor];
            [_attrButton setTitle:ASLocalizedString(@"关注")forState:UIControlStateNormal];
            ;
        }
        else
        {
            [_attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
            
            [_attrButton setFillColor:CLightGray];
        }
        
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
-(void)stopPlay
{
    [self.player1 stopPlay];
}
@end

